﻿namespace com.crownpack
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using com.crownpack.Extensions.SharePointExtensions;
    using com.crownpack.Extensions.SymbolExtensions;
    using com.crownpack.InitiativesDao;
    using com.crownpack.SharePoint;

    using Microsoft.SharePoint;

    using ReferenceDao = InitiativesDao.InitiativesConfigurationDao;

    using ReferenceObject = InitiativesDao.InitiativesConfiguration;

    public class InitiativesConfiguration_Definition_SiteLevel : CustomBaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "InitiativesConfiguration.xml";

        #endregion Fields

        #region Constructors

        public InitiativesConfiguration_Definition_SiteLevel( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~InitiativesConfiguration_Definition_SiteLevel( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Properties

        ContentTypeDefn InitiativesConfiguration
        {
            get
            {
                ReferenceObject c = null;
                return new ContentTypeDefn
                {
                    Name = Item.GetContentTypeName<ReferenceObject>( ),
                    ContentTypeId = Item.GetContentTypeId<ReferenceObject>( ),
                    Group = "com.crownpack.initiativesconfig",
                    Description = "Defines the configuration for the client initiatives.",
                    Fields = new FieldDefn[ ] {
                        new FieldDefn{ Name = Item.GetColumnName<ReferenceObject>( c.GetPropertySymbol( o=> o.ParentCustomerNo )), Required = true },
                        new FieldDefn{ Name = Item.GetColumnName<ReferenceObject>( c.GetPropertySymbol( o=> o.ClientSite )), Required = true },
                        new FieldDefn{ Name = Item.GetColumnName<ReferenceObject>( c.GetPropertySymbol( o=> o.SourceList )), Required = true },
                        new FieldDefn{ Name = Item.GetColumnName<ReferenceObject>( c.GetPropertySymbol( o=> o.Active ))},
                    }.Distinct( ).ToList( )
                };
            }
        }

        ListDefn InitiativesConfigurationList
        {
            get
            {
                return new ListDefn
                    {
                        RootFolderName = ReferenceDao.ListName,
                        Title = "Crown Initiatives Configuration",
                        Description = "Contains configuration definitions for client initiatives.",
                        ContentTypes = new string[ ]
                {
                    Item.GetContentTypeName<ReferenceObject>( )
                }.ToList( )
                    };
            }
        }

        #endregion Properties

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( GetFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( InitiativesConfiguration );
            CreateList( InitiativesConfigurationList );
            addEventReceiver( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( InitiativesConfigurationList, false );
            DeleteContentType( InitiativesConfiguration );
            DeleteSiteColumns( GetFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        void addEventReceiver( )
        {
            string assembly = Assembly.GetAssembly( typeof( InitiativesConfigurationItemEventReceiver ) ).FullName;
            string className = typeof( InitiativesConfigurationItemEventReceiver ).FullName;

            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( InitiativesConfigurationList.RootFolderName );

                List<SPEventReceiverDefinition> l = new List<SPEventReceiverDefinition>( );
                foreach ( SPEventReceiverDefinition v in spList.EventReceivers )
                    if ( ( v.Type == SPEventReceiverType.ItemAdding ||
                        v.Type == SPEventReceiverType.ItemUpdating ) &&
                        v.Assembly.Equals( assembly ) && v.Class.Equals( className ) )
                        l.Add( v );
                for ( int i = l.Count - 1; i >= 0; i-- )
                    l[ i ].Delete( );

                spList.EventReceivers.Add( SPEventReceiverType.ItemAdding, assembly, className );
                spList.EventReceivers.Add( SPEventReceiverType.ItemUpdating, assembly, className );
            }
        }

        #endregion Methods
    }
}